package com.dotstone.chipism.activity;

import java.util.ArrayList;

import com.dotstone.chipism.R;
import com.dotstone.chipism.imagepicker.CropImageView;
import com.dotstone.chipism.imagepicker.GlideImageLoader;
import com.dotstone.chipism.imagepicker.ImageGridActivity;
import com.dotstone.chipism.imagepicker.ImageItem;
import com.dotstone.chipism.imagepicker.ImagePicker;
import com.dotstone.chipism.util.NetUtil;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RebateActivity extends BaseActivity {
	private LinearLayout mBackL;
	private Button mSubmitBtn;
	private TextView mAccountTv;
	private EditText mCardEd;
	private EditText mBankEd;
	private EditText mBankEd1;
	private EditText mPhoneEd;
	private EditText mCodeEd;
	private Button mUploadBtn;
	private TextView mStatusBar;
	private String user_name;
	private ImagePicker imagePicker;
	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.nextstep:
			String card = mCardEd.getText().toString();
			String bank = mBankEd.getText().toString();
			String bank1 = mBankEd1.getText().toString();
			String phone = mPhoneEd.getText().toString();
			String code = mCodeEd.getText().toString();
			
			break;
		case R.id.upload_btn:
			imagePicker = ImagePicker.getInstance();
			 imagePicker.setImageLoader(new GlideImageLoader());// 图片加载器
			 imagePicker.setSelectLimit(1);// 设置可以选择几张
			 imagePicker.setMultiMode(false);// 是否为多选
			 imagePicker.setCrop(true);// 是否剪裁
			 imagePicker.setFocusWidth(1000);// 需要剪裁的宽
			 imagePicker.setFocusHeight(1000);// 需要剪裁的高
			 imagePicker.setStyle(CropImageView.Style.RECTANGLE);// 方形
			 imagePicker.setShowCamera(true);// 是否显示摄像
			
			 imagePicker.setMultiMode(false);
			 imagePicker.setSelectLimit(1);
			 imagePicker.setCrop(true);
			 imagePicker.setShowCamera(true);
			 Intent intent = new Intent(this, ImageGridActivity.class);
			 startActivityForResult(intent, 100);
			break;

		default:
			break;
		}
	}

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_rebate;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mBackL = $(R.id.back_layout);
		mSubmitBtn = $(R.id.nextstep);
		mSubmitBtn.setVisibility(View.VISIBLE);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mAccountTv = $(R.id.account_tv);
		mCardEd = $(R.id.card_ed);
		mBankEd = $(R.id.bank_ed);
		mBankEd1 = $(R.id.bank1_ed);
		mPhoneEd = $(R.id.phone_ed);
		mCodeEd = $(R.id.code_ed);
		mUploadBtn = $(R.id.upload_btn);
		boolVersion();
		mBackL.setOnClickListener(this);
		mSubmitBtn.setOnClickListener(this);
		mUploadBtn.setOnClickListener(this);
		user_name = (String)SPUtils.get(getApplicationContext(), "user_name", "");
		mAccountTv.setText(user_name);
		mPhoneEd.setText(user_name);
	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar
					.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
			if (data != null && requestCode == 100) {
				final ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
				Log.d("test", "file = " + images.get(0).path);
				if (!NetUtil.isNetworkAvailable(this)) {
					Toast toast = Toast.makeText(getApplicationContext(), R.string.nonetwork, Toast.LENGTH_SHORT);
					toast.show();
					return;
				}
				new Thread() {
					public void run() {
						try {
//							List<NameValuePair> pairs = new ArrayList<NameValuePair>();
//							// Prove_image
//							NameValuePair image3 = new BasicNameValuePair("userIcon", images.get(0).path);
//							NameValuePair strings1 = new BasicNameValuePair("token", (String) SPUtils.get(getApplicationContext(), "token", ""));
//							NameValuePair strings2 = new BasicNameValuePair("key", "10000");
//							NameValuePair strings3 = new BasicNameValuePair("reqeustTime", "" + System.currentTimeMillis());

//							pairs.add(image3);
//							pairs.add(strings1);
//							pairs.add(strings2);
//							pairs.add(strings3);
//							String ret = UploadUtil2.upload(HttpURL.UPLOAD_ICON_URL, pairs);
//							Log.d("test", "upload ret = " + ret);
//
//							JSONObject obj = new JSONObject(ret);
//							String resultCode = obj.getString("resultCode");
//							if (resultCode.equals("1")) {
//								runOnUiThread(new Runnable() {
//
//									@Override
//									public void run() {
//										String iconUrl = "file://" + images.get(0).path;
//										SPUtils.put(AccountManagerActivity.this, "iconUrl", iconUrl);
//										ImageLoader.getInstance().displayImage(iconUrl, mHeadImg, MyApplication.getLoaderOptions());
//										UserFragment.instance.initData();
//									}
//								});
//							} else {
//								throw new Exception();
//							}
						} catch (Exception e1) {
							e1.printStackTrace();
							runOnUiThread(new Runnable() {
								public void run() {
									ToastShow.Show(getApplicationContext(), "上传失败，请稍后再试");
								}
							});
						}
					};
				}.start();
			}
		}
	}

}
